<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="senderFormRules" :model="senderForm" label-position="left" label-width="140px">
      <el-form-item label="联系人" prop="fullname">
        <el-input v-model="senderForm.fullname" />
      </el-form-item>
      <el-form-item label="联系电话" prop="telephone">
        <el-input v-model="senderForm.telephone" />
      </el-form-item>
      <el-form-item label="区域" prop="title">
        <el-input v-model="senderForm.area" />
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input v-model="senderForm.address" />
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input v-model="senderForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        确认
      </el-button>
      <el-button v-if="senderForm.status.value === 5" type="danger" @click="handleDelete(senderForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveSender, removeSenderByID, getSenderDetail } from '@/api/logistics/sender'
import waves from '@/directive/waves' // waves directive
import Upload from '@/components/Upload/SingleImage2' // secondary package based on el-pagination
import { } from '@/utils'

export default {
  name: 'ComplexTable',
  components: { Upload},
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      pickerOptions: {
          disabledDate(time) {
            //return time.getTime() < Date.now();
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
      exchangeOptions:[],
      senderForm: {
        code: '',
        title: '',
        remark: '',
        keyword:[],
        status: {
          label: '',
          value: 1
        }
      },
      senderFormRules: [],
      keywordFormHidden:true,
      keywordForm:{
        title:""
      }
    }
  },
  created() {
    let senderID = this.$route.query && this.$route.query.senderID
    if (senderID !== undefined) {
      senderID = parseInt(senderID)
      if (!isNaN(senderID)) {
        this.listLoading = true
        const formData = {
          'senderID': senderID
        }
        getSenderDetail(formData).then(response => {
          if (response.status === 200) {
            this.senderForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(senderID)
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'sender' })
    },
    createData() {
      this.listLoading = true
      const senderForm = this.senderForm
      senderForm.status = senderForm.status.value
      console.log(senderForm)
      if (senderForm.ID > 0) {
        senderForm.senderID = senderForm.ID
      }
      saveSender(senderForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'sender' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      const fromdata = {
        'senderID': row.ID,
      }
      removeSenderByID(fromdata).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'sender' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
